package com.shangma.cn.utils;

import com.fasterxml.jackson.databind.ObjectMapper;

/**
 * 开发者：辉哥
 * 特点： 辉哥很帅
 * 开发时间：2021/7/1 17:54
 * 文件说明：
 */
public class JsonUtils {


    /**
     * 对象转字符串
     *
     * @param obj
     * @return
     */
    public static String obj2Str(Object obj) {
        try {
            ObjectMapper objectMapper = new ObjectMapper();
            String s = objectMapper.writeValueAsString(obj);
            return s;
        } catch (Exception e) {
            return "";
        }
    }


    public static <T> T str2Obj(String jsonStr, Class<T> clazz) {
        try {
            ObjectMapper objectMapper = new ObjectMapper();
            T t = objectMapper.readValue(jsonStr, clazz);
            return t;
        } catch (Exception e) {
            return null;
        }
    }
}
